package com.skin.admin.controller;


import com.skin.admin.common.FenyeSetting;
import com.skin.admin.entity.Fenye;
import com.skin.admin.entity.Roles;
import com.skin.admin.entity.Users;
import com.skin.admin.manager.RoleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author skin
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/roles")
public class RolesController {

    @Autowired
    private RoleManager roleManager;

    @RequestMapping(value = "/getAll")
    public ModelAndView getAll(HttpServletRequest request){
        Fenye f= FenyeSetting.setfenye(request);
        ModelAndView mv =new ModelAndView();
        mv.addObject("roles",roleManager.All(f.getCurrent(),f.getSize()));
        mv.setViewName("admin/roles/roles");
        return mv;
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable int id){
        if(id!=1){
            roleManager.deleteById(id);
        }
        return "redirect:/roles/getAll";
    }

    /**
     * 编辑
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable String id,Model model){
        Roles roles =roleManager.findBy("id",id);
        if(roles!=null){
            model.addAttribute("roles",roles);
            model.addAttribute("team",roleManager.All());
        }
        return "admin/roles/rolesedit";
    }
    /**
     * 编辑
     * @param roles
     * @return
     */
    @RequestMapping(value = "/editDo")
    public String editDo(Roles roles){
        roleManager.edit(roles);
        return "redirect:/roles/getAll";
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/insert")
    public String insert(Model model){
        model.addAttribute("team",roleManager.All());
        return "admin/roles/rolesinsert";
    }

    @RequestMapping(value = "/insertDo")
    public String insertDo(Roles roles){
        roleManager.insert(roles);
        return "redirect:/roles/getAll";
    }


}

